package com.ruoyi.zhgd.controller;

import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsWReportBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsWReportDetailVo;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsWGroupVo;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsWGroupBo;
import com.ruoyi.zhgd.service.INLaboratoryMechanicsWGroupService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 万能机主表信息
 *
 * @author tll
 * @date 2023-09-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/laboratoryMechanicsWGroup")
public class NLaboratoryMechanicsWGroupController extends BaseController {

    private final INLaboratoryMechanicsWGroupService iNLaboratoryMechanicsWGroupService;

    /**
     * 查询万能机主表信息列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<NLaboratoryMechanicsWGroupVo> list(NLaboratoryMechanicsWGroupBo bo, PageQuery pageQuery) {
        return iNLaboratoryMechanicsWGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出万能机主表信息列表
     */
    @SaCheckLogin
    @Log(title = "万能机主表信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NLaboratoryMechanicsWGroupBo bo, HttpServletResponse response) {
        List<NLaboratoryMechanicsWGroupVo> list = iNLaboratoryMechanicsWGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "万能机主表信息", NLaboratoryMechanicsWGroupVo.class, response);
    }

    /**
     * 获取万能机主表信息详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<NLaboratoryMechanicsWGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                                   @PathVariable Long id) {
        return R.ok(iNLaboratoryMechanicsWGroupService.queryById(id));
    }

    /**
     * 新增万能机主表信息
     */
    @SaCheckLogin
    @Log(title = "万能机主表信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NLaboratoryMechanicsWGroupBo bo) {
        return toAjax(iNLaboratoryMechanicsWGroupService.insertByBo(bo));
    }

    /**
     * 修改万能机主表信息
     */
    @SaCheckLogin
    @Log(title = "万能机主表信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NLaboratoryMechanicsWGroupBo bo) {
        return toAjax(iNLaboratoryMechanicsWGroupService.updateByBo(bo));
    }

    /**
     * 删除万能机主表信息
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "万能机主表信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iNLaboratoryMechanicsWGroupService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 万能机设备信息分页
     */
    @SaCheckLogin
    @GetMapping("/page")
    public TableDataInfo<NLaboratoryMechanicsWGroupVo> page(NLaboratoryMechanicsWGroupBo bo, PageQuery pageQuery) {
        return iNLaboratoryMechanicsWGroupService.page(bo, pageQuery);
    }

    /**
    * 验证采集的数据是否合格 verifyEligible
    * @author cj
    * @date 2023/11/15 9:49
    * @return
    **/
    @SaIgnore
    @GetMapping("/verifyEligible")
    public R verifyEligible(@RequestParam Integer id) {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
        executor.schedule(() -> iNLaboratoryMechanicsWGroupService.verifyEligible(id), 120, TimeUnit.SECONDS);
        return R.ok();
    }

    /**
     * 验证采集的数据是否合格 verifyEligible
     * @author cj
     * @date 2023/11/15 9:49
     * @return
     **/
    @SaIgnore
    @GetMapping("/verifyEligiblesyn")
    public R verifyEligiblesyn(@RequestParam Integer id) {
        iNLaboratoryMechanicsWGroupService.verifyEligible(id);
        return R.ok();
    }

    /**
     * 获取万能机报表列表信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/reports")
    public R<List<NLaboratoryMechanicsWReportDetailVo>> getReportInfos(@RequestParam("id") Long id, @RequestParam("symc") String symc) {
        if(StringUtils.isEmpty(symc)||id == null){
            return null;
        }
        return iNLaboratoryMechanicsWGroupService.getReportInfos(id, symc);
    }

    /**
     * 获取万能机报表信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/report")
    public R<List<NLaboratoryMechanicsWReportDetailVo>> getReportInfo(@RequestParam("id") Long id, @RequestParam("symc") String symc) {
        if (StringUtils.isEmpty(symc) || id == null) {
            return R.fail("参数不够");
        }
        if (symc.equals("钢筋焊接")) {
            return iNLaboratoryMechanicsWGroupService.gethjReportInfo(id, symc);
        } else if (symc.equals("热轧带肋钢筋") || symc.equals("钢筋原材")) {
            return iNLaboratoryMechanicsWGroupService.getReportInfo(id, symc);
        } else if (symc.equals("钢筋机械连接")) {
            return iNLaboratoryMechanicsWGroupService.getReportInfo(id, symc);
        }
        return iNLaboratoryMechanicsWGroupService.getReportInfo(id, symc);
    }

    /**
     * 保存万能机报表信息
     *
     * @param bo 主键
     */
    @SaCheckLogin
    @PostMapping("/saveReport")
    public R saveReport(@RequestBody NLaboratoryMechanicsWReportBo bo) {

        return iNLaboratoryMechanicsWGroupService.saveReport(bo);
    }

    /**
     * 钢筋焊接保存万能机报表信息
     *
     * @param bo 主键
     */
    @SaCheckLogin
    @PostMapping("/hjsaveReport")
    public R hjsaveReport(@RequestBody NLaboratoryMechanicsWReportBo bo) {

        return iNLaboratoryMechanicsWGroupService.hjsaveReport(bo);
    }

    /**
     * 钢筋机械连接保存万能机报表信息
     *
     * @param bo 主键
     */
    @SaCheckLogin
    @PostMapping("/jxljsaveReport")
    public R jxljsaveReport(@RequestBody NLaboratoryMechanicsWReportBo bo) {

        return iNLaboratoryMechanicsWGroupService.jxljsaveReport(bo);
    }


}
